'''
@auther tianxiaoxi  
@file user by
@time 2021/12/23 9:06
'''

import requests
import app
from api.login import LoginAPI


class UserAPI:
    def __init__(self):
        # 定义url
        self.user_list_url = app.BASE_URL + "/api/sys/user?page={}&size={}"
        self.add_user_url = app.BASE_URL + "/api/sys/user"
        self.update_user_url = app.BASE_URL + "/api/sys/user/{}"
        self.select_user_url = app.BASE_URL + "/api/sys/user/{}"
        self.delete_user_url = app.BASE_URL + "/api/sys/user/{}"

        # 判断是否有TOKEN, 没有的话添加TOKEN 定义 headers的内容
        if app.TOKEN is None:
            login = LoginAPI()
            login.token_login()
            token = "Bearer " + app.TOKEN
            app.headers["Authorization"] = token

        else:
            token = "Bearer " + app.TOKEN
            app.headers["Authorization"] = token

    # 员工列表
    def user_list(self, page, size):
        return requests.get(self.user_list_url.format(page, size), headers=app.headers)

    # 添加员工
    def user_add(self, json):
        print(app.headers)
        print(json)
        return requests.post(self.add_user_url, headers=app.headers, json=json)  # 添加员工

    # 修改
    def user_update(self, target, json):
        return requests.put(url=self.update_user_url.format(target), headers=app.headers, json=json)

    # 查询
    def user_select(self, target):
        return requests.get(self.select_user_url.format(target), headers=app.headers)

    # 删除
    def user_delete(self, target):
        return requests.delete(self.delete_user_url.format(target), headers=app.headers)
